﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using System.Data;

namespace Joyintech.Admin.inpiao365.general.system.area
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            { 
                //绑定部门（如果是修改部门则需排除其自身及其下级部门）
                DataBindArea();

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("id") != "")
                {
                    DataBindForm();
                }
            }
        }

        #region 绑定部门（如果是修改部门则需排除其自身及其下级部门）

        /// <summary>
        /// 绑定所有部门
        /// </summary>
        private void DataBindArea()
        {
            this.ddlpreencode.Items.Clear();

            this.ddlpreencode.Items.Add(new ListItem("作为一级行政区划", "0"));

            List<systemArea> list = new List<systemArea>();

            if (DNTRequest.GetQueryString("id") != "")
            {
                list = new systemArea_BLL().GetModelList("preencode='' and id<>'" + DNTRequest.GetQueryString("id") + "'");
            }
            else
            {
                list = new systemArea_BLL().GetModelList("preencode=''");
            }

            foreach (systemArea model in list)
            {
                this.ddlpreencode.Items.Add(new ListItem(model.Name, model.Encode.ToString()));

                //递归绑定下级部门
                DataBindAreaChild(model.Encode, "--");
            }
        }

        /// <summary>
        /// 递归绑定下级部门
        /// </summary>
        /// <param name="menuPID"></param>
        private void DataBindAreaChild(string encode, string frontStr)
        {
            List<systemArea> list = new List<systemArea>();

            if (DNTRequest.GetQueryString("id") != "")
            {
                list = new systemArea_BLL().GetModelList("preencode=" + encode + " and id<>'" + DNTRequest.GetQueryString("id") + "'");
            }
            else
            {
                list = new systemArea_BLL().GetModelList("preencode=" + encode);
            }

            foreach (systemArea model in list)
            {
                this.ddlpreencode.Items.Add(new ListItem(frontStr + model.Name, model.Encode.ToString()));

                //递归绑定下级部门
                DataBindAreaChild(model.Encode, "--" + frontStr);
            }
        }

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new systemArea_BLL().GetList("id='" + DNTRequest.GetQueryString("id") + "'");

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表Area
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            systemArea model = new systemArea();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("id") != "")
            {
                model.Id = new Guid(DNTRequest.GetQueryString("id"));

                dataResult = new systemArea_BLL().Update(model);
            }
            else
            {
                dataResult = new systemArea_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");                
            }
        }

        #endregion
    }
}